
//
//  FHEmoticonToolBar.swift
//  WeiBo
//
//  Created by qfh on 16/7/19.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

enum CZEmoticonToolBarButtonType: Int {
    //  枚举的原始值0做为我们视图的tag的时候需要注意
    //  默认
    case Normal = 1000
    //  Emoji
    case Emoji = 1001
    //  浪小花
    case Lxh = 1002
}

class FHEmoticonToolBar: UIStackView {

   
    private var selectButton: UIButton?
    
    
    var callBack: ((type: CZEmoticonToolBarButtonType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        
        axis = .Horizontal
        
        distribution = .FillEqually
    
        addclidButton("默认", imageName: "compose_emotion_table_left", type: .Normal)
        
        addclidButton("Emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        
        addclidButton("浪小花", imageName: "compose_emotion_table_right", type: .Lxh)
        

        
        
    }
    
    /**
     添加子控件
     
     - parameter name:      <#name description#>
     - parameter imageName: <#imageName description#>
     - parameter type:      <#type description#>
     */
    private func addclidButton(name: String, imageName: String, type: CZEmoticonToolBarButtonType) {
    
        let button = UIButton()
        
        button.setTitle(name, forState: .Normal)
        
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.setBackgroundImage(UIImage(named: imageName + "_normal"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: imageName + "_selected"), forState: .Selected)
        
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        
        button.adjustsImageWhenHighlighted = false
        
        button.tag = type.rawValue
        
        addArrangedSubview(button)
        
        if type == .Normal {
        
            selectButton = button
            
            button.selected = true
        
        }
    
        
    
    }
    
    // MARK: - button 点击事件
    
    @objc private func buttonAction(btn: UIButton) {
    
        
        if selectButton == btn {
        
            return
        }
        
        
        btn.selected = true
        
        selectButton?.selected = false
    
        selectButton = btn
        
    
        let type = CZEmoticonToolBarButtonType(rawValue: btn.tag)!
        
        
        callBack?(type: type)
        
        
    }
    
    func selectedButtonWidthSection(section: Int) {
    
        let button = viewWithTag(section + 1000) as! UIButton
        
        if selectButton == button {
        
            return
        }
        
        selectButton?.selected = false
        
        button.selected = true
        
        selectButton = button
    
    }
    
    

}
